<?php

/**
 * @author aisyah
 * @copyright 2012
 */
session_start();

require_once "order.php";

class Order_Dao{
    
    var $class_db;
    var $shipping;
    var $shipping_cost;
    var $subtotal;
    var $total;
    var $order = array();
    
    public function __construct(Database $db){
        
        $this->class_db = $db;
    }
    
    //input ke database order_detail
    function add_order($order = array()){
        
        foreach($order as $data){
            
            //menambahkan di order detail
            $value = array($data->order_id,$data->product,$data->store,$data->price, $data->jumlah, $data->fix_price);
            $row = "order_id,product, store, price, jumlah, total";
            
            $this->class_db->add('order_detail',$value, $row);
            
            //update kolom count_browse
            $this->class_db->hasil = "";
            $this->class_db->show_Data('product_detail','count_order',null,null,'productdet_id='.$data->product);
            $pid = $this->class_db->hasil;
            
            foreach($pid as $hasil){
                $pid = $hasil['count_order'];
            }
            
            $pid++;
            
            $this->class_db->update('product_detail',array('count_order'=>$pid),'productdet_id='.$data->product);
            
            unset($_SESSION['cart']);
            unset($_SESSION['alamat']);
            unset($_SESSION['kirim']);
            unset($_SESSION['bayar']);
            unset($_SESSION['biaya_kirim']);
            
            header('location:index.php');
            
        }
    }
    
    //memindahkan session ke objek order
    function to_order(){
        
        $order_id = $this->get_order_id();
        
        foreach(array_keys($_SESSION['cart']) as $key){
            $order = new Order;
            
            $order->order_id = $order_id;
            $order->product = $_SESSION['cart'][$key]['productid'];
            $order->store = $this->get_store($order->product);
    		$order->jumlah = $_SESSION['cart'][$key]['qty'];
            $order->price = $_SESSION['cart'][$key]['prc'];
            $order->fix_price = $order->price*$order->jumlah;   
            
            $this->order[] = $order;                 
               
       }
        
    }
    
    function get_customer(){
        
        $login = $_SESSION['loggedinuser'];
        //print_r($login);
        
        $this->class_db->hasil = "";
        $this->class_db->show_data("user","user_id",null,null,"username='".$login."'");
        $hasil = $this->class_db->hasil;
        foreach ($hasil as $data){
            $uid = $data['user_id'];
        }
        
        //print_r($uid);
        
        $this->class_db->hasil = "";
        $this->class_db->show_data("customer","customer_id",null,null,"user_id=".$uid);
        $hasil2 = $this->class_db->hasil;
        foreach ($hasil2 as $data){
            $customer = $data['customer_id'];
            //$customer = $data['name'];
        }
        //print_r($customer);    
        
        
        return $customer;
    }
    
    
    function get_store($product_id){     
        
        $this->class_db->hasil = "";
        $this->class_db->show_data('product_detail','store_id',null,null,'productdet_id='.$product_id);
        $hasil = $this->class_db->hasil;
        foreach ($hasil as $data){
            $store = $data['store_id'];
        }
        
        return $store;
    }
    
    //belum selesai
    function get_payment(){
        
        $payment = $_SESSION['bayar'];
        
        return $payment;
    }
    
    
    function get_ship_addr(){
        
        $alamat = array();
        $alamat['name'] = $_SESSION['alamat']['name'];
        $alamat['street'] = $_SESSION['alamat']['street'];
        $alamat['kecamatan'] = $_SESSION['alamat']['kecamatan'];
        $alamat['city'] = $_SESSION['alamat']['city'];
        $alamat['province'] = $_SESSION['alamat']['province'];
        $alamat['zip_code'] = $_SESSION['alamat']['zip_code'];
        
        $this->class_db->hasil="";
        
        $this->class_db->show_Data('t_city','city_name',null,null,'city_id='.$alamat['city']);
        $city = $this->class_db->hasil;
        //print_r($city);
        foreach($city as $hasil){
            $alamat['city'] = $hasil['city_name'];
        }
        
        $this->class_db->hasil="";
        $this->class_db->show_Data('t_province','name',null,null,'province_id='.$alamat['province']);
        $prov = $this->class_db->hasil;
        //print_r($prov);
        foreach($prov as $hasil){
            $alamat['province'] = $hasil['name'];
        }
        $alamat= implode('</br>',$alamat);
        
        return $alamat;
    }
    
    function get_subtotal(){
        
        foreach(array_keys($_SESSION['cart']) as $key){       
            
       
            $this->subtotal += $_SESSION['cart'][$key]['prc'];       
       
        
       }
        return $this->subtotal;
    }
    
    //total biaya kirim
    function get_shipping(){
        
        $count = 0;
        foreach(array_keys($_SESSION['cart']) as $key){       
            
            $count++;       
               
       }
       
       
       $this->shipping = $_SESSION['kirim'];
       
       $this->shipping_cost = $count*$_SESSION['biaya_kirim'];
        
        return $this->shipping_cost;
    }
    
    //total biaya yang harus dibayar
    function get_total(){
        
        $this->total = $this->subtotal+$this->shipping_cost;
        
        return $this->total;
    }
    
    //mendapatkan 
    function get_order_id(){
        
        $query = "SELECT MAX(order_id) as id FROM corder";
        
        $result = mysql_query($query);
        
        while($r=mysql_fetch_array($result)){
            $id= $r['id'];
        }
        
        return $id;
    }
    
    //mendapatkan nomor po -- belum selesai
    function po_number(){
        
        $this->class_db->hasil = "";
        $this->class_db->show_Data('corder','MAX(order_id) as order_id');
        
        $order = $this->class_db->hasil;
        
        foreach($order as $data){
            
            $order = (int)$data['order_id']+1;
        }
        
        
        $max = 5;
        
        $order = (string)$order;
        
        $length_string = strlen($order);
        
        for($i=0; $i<($max-$length_string);$i++){
            $order = "0".$order;
        }
        //$po_number = $this->acak_nomor($order);
        $po_number = "PO-".$order;
        
        return $po_number;
    }
    
    function add_cart($id, $cat=null, $checkout=null){      
        
        
        if(!is_null($cat)){
            if($cat==1){
                $modul = "tkering";
            }else if($cat==2){
                $modul = "tbasah";
            }  else if($cat==3){
                $modul = "mkering";
            }else if($cat==4){
                $modul = "mbasah";
            }else if($cat==5){
                $modul = "semua";
            }
        }        
             
        $this->class_db->show_Data('product_detail','price',null,null,'productdet_id='.$id);
        $hasil = $this->class_db->hasil;
        
        foreach($hasil as $data){
            $hasil = $data['price'];
        }  
                
        if(is_array($_SESSION['cart'])){
            if(isset($_SESSION['cart'][$id])){
                $_SESSION['cart'][$id]['qty']++;
                $_SESSION['cart'][$id]['prc'] += $hasil;
            }else{
                $_SESSION['cart'][$id]['productid']=$id;
        		$_SESSION['cart'][$id]['qty'] = 1;
                $_SESSION['cart'][$id]['prc'] = $hasil;
            }		 		         
            
    	}
    	else{
    		$_SESSION['cart']=array();
    		$_SESSION['cart'][$id]['productid']=$id;
    		$_SESSION['cart'][$id]['qty']=1;
            $_SESSION['cart'][$id]['prc'] = $hasil;
    	}
                   
            
        if(!is_null($checkout)){
            header('location:cart_view.php');
        }elseif(!is_null($cat)){
            header('location:cat_view.php?modul='.$modul);
        }else{
            header('location:index.php');
        }
        
        
    }
    
    function substract_cart($modul, $id){
        $this->class_db->show_Data('product_detail','price',null,null,'productdet_id='.$modul);
        
        foreach($this->class_db->hasil as $data){
            $price = $data['price'];
        }
    
        if($_SESSION['cart'][$id]['qty']>1){
            $_SESSION['cart'][$id]['qty']--;
            $_SESSION['cart'][$id]['prc']-=$price;
        }else{
            unset($_SESSION['cart'][$id]);
            unset($_SESSION['alamat']);
            unset($_SESSION['kirim']);
            unset($_SESSION['bayar']);
        }
        
        header('location:cart_view.php');
    }
    
    function delete_cart($id){
        unset($_SESSION['cart'][$id]);
        unset($_SESSION['alamat']);
        unset($_SESSION['kirim']);
        unset($_SESSION['bayar']);
        
        if(count($_SESSION['cart'])<1){
            unset($_SESSION['cart']);
            header('location:index.php');
        }else{
            header('location:cart_view.php');
        }
    }
    
    function cek_city_prov_id($value, $city_prov){
        
        $result;
        
        if(!is_int($value)){
            if($city_prov==2){
                $q = "SELECT city_id FROM t_city WHERE city_name='".$value."'";
            }elseif($city_prov==1){
                $q = "SELECT province_id FROM t_province WHERE name='".$value."'";
            }
            //print_r($q);                   
                $h = $this->class_db->select_table($q);
                //print_r($h);
                foreach($h as $i){
                    if($city_prov==2){
                        $result = $i['city_id'];
                    }elseif($city_prov==1){
                        $result = $i['province_id'];
                    }
                    
                }
            }else{
                $result=$value;
        }
        print_r($result);
        return $result;
    }
    
    
    
}

